define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiluhouse/current/message/index' + location.search,
                    add_url: 'xiluhouse/current/message/add',
                    edit_url: 'xiluhouse/current/message/edit',
                    del_url: 'xiluhouse/current/message/del',
                    multi_url: 'xiluhouse/current/message/multi',

                    user_url: 'xiluhouse/user/user/index',
                    house_url: 'xiluhouse/house/house/index',

                    table: 'message',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='house_id']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.house_url).data("primaryKey", "id").data("field", "name");
                $("input[name='to_user_id']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.user_url).data("primaryKey", "id").data("field", "nickname");

            })
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3'),"4":__('Type 4')}, formatter: Table.api.formatter.normal},
                        
                        {field: 'house_id', title: __('House_id'),formatter: function (value, row, index) {
                            if(row.house_id>0){
                                var html='';
                                var url =$.fn.bootstrapTable.defaults.extend.house_url+ "?id=" + row.house_id;
                                html+='<a  href="' + url + '" class="bg-info btn-dialog" title="查看关联信息"> 【'+ row.house_id +'】'+row.house.name+' </a>';
                                return html;
                            }else{
                                return '-';
                            }
                        }
                        },
                        {field: 'to_user_id', title: __('To_user_id'),formatter: function (value, row, index) {
                            if(row.to_user_id>0){
                                var html='';
                                var url =$.fn.bootstrapTable.defaults.extend.user_url+ "?id=" + row.to_user_id;
                                html+='<a  href="' + url + '" class="bg-info btn-dialog" title="查看关联信息"> 【'+ row.to_user_id +'】'+row.user.nickname+'<br/>'+row.user.mobile+' </a>';
                                return html;
                            }else{
                                return '所有人';
                            }
                        }
                        },
                        {field: 'title', title: __('Title'),operate:'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'xiluhouse/current/message/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'xiluhouse/current/message/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'xiluhouse/current/message/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});